const BASE_URL = 'https://www.youngcr.cn/capi'

class Request {
  constructor(baseUrl) {
    this.baseUrl = baseUrl
  }

  /**
   * @description GET请求
   * @param { String } url 请求地址
   * @param { String } data 请求params
   */
  get(url, params) {
    return new Promise((resolve, reject) => {
      wx.request({
        method: 'GET',
        data: params,
        url: `${this.baseUrl}${url}`,
        success: (result) => {
          resolve(result.data)
        },
        fail: (error) => {
          reject(error)
        }
      })
    })
  }

  /**
   * @description POST请求
   * @param { String } url 请求地址
   * @param { String } data 请求体
   */
  post(url, data) {
    return new Promise((resolve, reject) => {
      wx.request({
        method: 'POST',
        url: `${this.baseUrl}${url}`,
        data,
        success: (result) => {
          resolve(result.data)
        },
        fail: (error) => {
          reject(error)
        }
      })
    })
  }
}

const request = new Request(BASE_URL)

export default request